package com.four.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.pojo.TicketTemplate;
import com.four.secondcache.RedisCache;
import com.four.service.TicketTemplateService;
import com.four.mapper.TicketTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author live
* @description 针对表【ticket_template】的数据库操作Service实现
* @createDate 2023-01-22 21:57:15
*/
@Service
public class TicketTemplateServiceImpl extends ServiceImpl<TicketTemplateMapper, TicketTemplate>
        implements TicketTemplateService{

    @Autowired
    private TicketTemplateMapper templateMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public TicketTemplate findById(Integer id) {
        return templateMapper.findById(id);
    }

    @Override
    public void createTicketTemplate(TicketTemplate ticketTemplate) {
        redisCache.clear();

        templateMapper.createTicketTemplate(ticketTemplate);
    }

    @Override
    public List<TicketTemplate> findAvailableTicket() {
        List<TicketTemplate> ticketTemplates = templateMapper.findAvailableTicket();

        List<TicketTemplate> availableTicket =
                ticketTemplates
                .stream()
                .filter(at -> at.getShowTime().minusHours(3).isAfter(LocalDateTime.now()))
                .collect(Collectors.toList());

        return availableTicket;
    }
}
